import { AxiosAdapter } from "./adapter/AxiosAdapter";
import { CallFactory } from "./CallFactory";
import { BASE_URL, CALL_FACTORY, REST_SERVICE } from "./Constant";
import { HttpAdapter } from "./HttpAdapter";

export class Builder {
  baseUrl: string

  httpAdapter: HttpAdapter;

  constructor() {
    this.httpAdapter = new AxiosAdapter()
  }

  build() {
    return new RESTfulHttp(this);
  }
}

export class RESTfulHttp {
  private _baseUrl: string

  private _httpAdapter: HttpAdapter;

  constructor(build: Builder) {
    this._baseUrl = build.baseUrl;
    this._httpAdapter = build.httpAdapter;
  }

  static createBuild() {
    return new Builder();
  }

  createService<T>(service: T) :  T {
    if (service[REST_SERVICE] != true) {
      throw new Error("Not a RESTful service");
    }
    service[CALL_FACTORY] = new CallFactory(this._httpAdapter);
    service[BASE_URL] = this._baseUrl
    return service
  }
}